//
//  SupplyCommodityTableViewCell.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/29.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "SupplyCommodityTableViewCell.h"

@implementation SupplyCommodityTableViewCell

- (void)awakeFromNib {
    
    _kk_itemNameLabel.contentAlignment = KKLabelContentAlignmentTop;
}

#pragma mark - UITextField methods

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == _kk_itemPriceField) {
        
        double price = [textField.text doubleValue];
        [_kk_item setKk_price: @(price)];
        
        [self updateItemData];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return true;
}

#pragma mark - Private methods

- (void)updateItemData {
    
    _kk_itemPriceField.text = [NSString stringWithFormat: @"%.2f", [_kk_item.kk_price doubleValue]];
    _kk_itemProfitField.text = [NSString stringWithFormat: @"%.2f", [_kk_item profitAmount]];
    _kk_itemTotalProfitField.text = [NSString stringWithFormat: @"共盈利: %.2f", [_kk_item totalProfitAmount]];
    
    [[NSNotificationCenter defaultCenter] postNotificationName: NSNotificationNameIsSupplyCommodityListAmountName object: nil];
}

#pragma mark - Setter methods

- (void)setKk_item:(KKSupplyCommodityItem *)kk_item {
    
    _kk_item = kk_item;
    _kk_itemNameLabel.text = _kk_item.kk_name;
    
    NSInteger number = [_kk_item.kk_number integerValue];
    
    _kk_numberLabel.text = [NSString stringWithFormat: @"%@",@(number)];
    _kk_reduceButton.enabled = number;
    
    [self updateItemData];
}

#pragma mark - Action methods

- (IBAction)actionOfCountReduce:(UIButton *)sender {
    
    NSInteger number = [_kk_item.kk_number integerValue];
    number--;
    
    [_kk_item setKk_number: @(number)];
    
    _kk_numberLabel.text = [NSString stringWithFormat: @"%@",@(number)];
    _kk_reduceButton.enabled = number;
    
    [self updateItemData];
}

- (IBAction)actionOfCountIncrease:(UIButton *)sender {
    
    NSInteger number = [_kk_item.kk_number integerValue];
    number++;
    
    [_kk_item setKk_number: @(number)];
    
    _kk_numberLabel.text = [NSString stringWithFormat: @"%@",@(number)];
    _kk_reduceButton.enabled = number;
    
    [self updateItemData];
}

@end
